package com.shanshi.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shanshi.activity.tool.OkHttpTool;
import com.shanshi.activity.tool.PhoneNum;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;


public class ChangeTelphone extends Activity {

	private Button sure;
	private TextView tv,tv_title;
	private EditText et_num,et_getnum;
	private EventHandler ev;
	private String userId,tel;
	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor editor;
	public static final String postMessage="http://192.168.31.248:8080/SsProject/app/login";
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				if (msg.arg1 == 0) {
					Toast.makeText(ChangeTelphone.this,"获取验证码成功", Toast.LENGTH_SHORT).show();
				} else if (msg.arg1 == 1) {
					SMSSDK.unregisterEventHandler(ev);
					String reqbody=gson.toJson(tel);
					OkHttpTool.doPut(OkHttpTool.BASE_URL + "users/" + userId + "+/phoneNum", reqbody, new OkHttpTool.setOkhttpListener() {
						@Override
						public void onSuccess(String s) {
							finish();
						}

						@Override
						public void onFail() {
							Toast.makeText(ChangeTelphone.this,"修改失败", Toast.LENGTH_SHORT).show();
						}
					});
				} else if (msg.arg1 == 2) {
					Toast.makeText(ChangeTelphone.this,"验证错误", Toast.LENGTH_SHORT).show();
				}
			}
		}
	};
	private Gson gson = new Gson();


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		tv_title= (TextView) findViewById(R.id.title);
		tv_title.setText("更换号码");
		sharedPreferences=this.getSharedPreferences("share",MODE_PRIVATE);
		editor=sharedPreferences.edit();
		userId=sharedPreferences.getString("userId","");
		cn.smssdk.SMSSDK.initSDK(this, "1bfd57d60c65c", "118b16e56b29808adf8d684b2c695007");
		sure= (Button) findViewById(R.id.login_sure);
		et_num= (EditText) findViewById(R.id.tel);
		et_getnum= (EditText) findViewById(R.id.verificationCode);
		tv= (TextView) findViewById(R.id.getVerificationCode);
		tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (PhoneNum.isPhone(et_num.getText().toString())) {
					SMSSDK.getVerificationCode("86", et_num.getText().toString());
					new CountDownTimer(60000, 1000) {
						@Override
						public void onTick(long millisUntilFinished) {
							tv.setText("剩余" + (millisUntilFinished / 1000) + "S");
							tv.setEnabled(false);
						}

						@Override
						public void onFinish() {
							tv.setText("获取验证码");
							tv.setEnabled(true);
						}
					}.start();
				}else {
					Toast.makeText(ChangeTelphone.this,"请填写正确手机号码",Toast.LENGTH_SHORT).show();
				}
			}
		});
		sure.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SMSSDK.submitVerificationCode("86",
						et_num.getText().toString(), et_getnum.getText()
								.toString());
				register();

			}
		});

	}


	public void nextPage(View view) {
		Intent nextIntent = null;
		switch (view.getId()) {
			case R.id.fanhui:
				nextIntent = new Intent(this, IndexActivity.class);
				break;
//		case R.id.login_sure:
//			nextIntent = new Intent(this, SpreadMessage.class);
//		break;
			case R.id.agreement:
				nextIntent = new Intent(this,LoginActivity.class);
				break;
		}
		startActivity(nextIntent);
	}
	private void sendMessage(int what, int arg) {
		Message message = Message.obtain();
		message.what = what;
		message.arg1 = arg;
		handler.sendMessage(message);
	}


	private void register() {
		ev = new EventHandler() {

			@Override
			public void afterEvent(int event, int result, Object data) {
				if (result == SMSSDK.RESULT_COMPLETE) { // 回调完成
					// 提交验证码成功,如果验证成功会在data里返回数据。data数据类型为HashMap<number,code>
					if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
						HashMap<String, Object> mData = (HashMap<String, Object>) data;
						String country = (String) mData.get("country");// 返回的国家编号
						tel = (String) mData.get("phone");// 返回用户注册的手机号
						editor.putString("phoneNum",tel);
						sendMessage(0,1);
					} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {// 获取验证码成功
						sendMessage(0,0);
					} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {// 返回支持发送验证码的国家列表

					}
				} else {
					sendMessage(0,2);
					((Throwable) data).printStackTrace();
				}
			}
		};
		SMSSDK.registerEventHandler(ev); //注册短信回调监听
	}



}
